// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success (res) {
		  console.log(res)
		  console.log(res.code)
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'http://127.0.0.1:8088/mini/login',
            data: {
              code: res.code
            },
            success:(res)=>{
              const {flag,data,message}=res.data;
              if(!flag){
                return  wx.showToast({
                  title: message,
                  icon: 'error',
                  duration: 2000

                });
              }
              wx.setStorageSync('token', `${data.tokenHead} ${data.token}`);
              wx.setStorageSync('userInfo', data.userinfo);
              wx.setStorageSync('openId', data.openid);


              wx.getWeRunData({
                  success :(res)=> {
                    console.log(res,"sasa")
                    // 拿 encryptedData 到开发者后台解密开放数据
                    const {encryptedData,iv} = res
                    wx.request({
                      url: 'http://127.0.0.1:8088/mini/werun',
                      method:'POST',
                      data:{
                        encryptedData:encryptedData,
                        iv:iv,
                        sessionKey:data.sessionKey,
                        openid:data.openid
                      },
       
                      header:{
                        'Authorization':wx.getStorageSync('token')              
                      },
                      success:(result)=>{
                          console.log(result);
                          console.log(result.data.data)
                          wx.setStorageSync('step', result.data.data)
                          console.log(wx.getStorageSync('step'),"21")
                      },
                      fail:(err)=>{
                        wx.showToast({
                          title: '请关注“微信运动”公众号后重试',
                          icon: 'none',
                          duration: 3000
                        })
                      }
                    })
    
                  }
              })
   

            },
            fail:(err)=>{
               console.log('接口请求失败：--》',err);
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  globalData: {
    userInfo: null
  },
  towxml:require('/towxml/index'),
})
